var treeObj;
var ztree_root_id = -1;
var treeId = 'handle_tree'
/*var zNodes =[
	{ id:ztree_root_id, pId:0, name:"数据预处理", open:true}
];*/
var zNodes = [
    {
        id: '0', name: "数据预处理", open: true, ico: 'ico-folder-open',
        children: [
            {id: '1', name: "类型转换", isParent: false, ico: 'ico-click'},
            {id: '2', name: "添加序号列", isParent: false, ico: 'ico-click'},
            {id: '3', name: "拆分", isParent: false, ico: 'ico-click'},
            {id: '4', name: "缺失值补充", isParent: false, ico: 'ico-click'},
            {id: '5', name: "归一化", isParent: false, ico: 'ico-click'},
            {id: '6', name: "标准化", isParent: false, ico: 'ico-click'}
        ]
    }

];

function addDiyDom(treeId, treeNode) {
    var aObj = $("#" + treeNode.tId + "_a");
    if ($("#diySpan_" + treeNode.id).length > 0) return;
    var editStr = "<span id='diySpan_" + treeNode.id + "' class='ztree-node-icon " + treeNode.ico + "'> </span>";
    aObj.prepend(editStr);
};

var setting = {
    edit: {
        enable: false,
        showRenameBtn: false,
        showRemoveBtn: false
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        beforeClick: zTreeBeforeClick
    },
    view: {
        showLine: false,
        showTitle: false,
        showIcon: false,
        selectedMulti: false,
        addDiyDom: addDiyDom
    }
};

$(document).ready(function () {
    treeObj = $.fn.zTree.init($("#" + treeId), setting, zNodes);
});

//规避删除冒泡
function zTreeBeforeClick(treeId, treeNode, clickFlag) {
    if (!treeNode) return false;
}

//点击节点回调
function zTreeOnDrap(event, treeId, treeNode) {
    console.log(treeNode)
}

//节点被折叠
function zTreeOnCollapse(event, treeId, treeNode) {

}

//节点被展开
function zTreeOnExpand(event, treeId, treeNode) {

}

//添加绑定成功标识
function addIcon(treeNode) {
    var aObj = $("#" + treeNode.tId + "_a");
    if ($("#sucIcon_" + treeNode.id).length > 0) return;
    var iconStr = "<span type='button' class='suc-icon mybtn' id='sucIcon_" + treeNode.id
        + "' ></span>";
    aObj.append(iconStr);
}

//移除成功标志
function removeIcon(treeNode) {
    $("#sucIcon_" + treeNode.id).remove();
}

//添加编辑按钮
function addEidtBtn(treeId, treeNode) {
    var aObj = $("#" + treeNode.tId + "_a");
    if ($("#editBtn_" + treeNode.id).length > 0) return;
    var editStr = "<span type='button' class='button edit mybtn' id='editBtn_" + treeNode.id
        + "' title='编辑' ></span>";
    aObj.append(editStr);
    var btn = $("#editBtn_" + treeNode.id);
    if (btn) btn.bind("click", function () {
        core.initBlock(treeNode.block_html_id);
    });
}

//添加删除按钮
function addDelBtn(treeId, treeNode) {
    var aObj = $("#" + treeNode.tId + "_a");
    if ($("#delBtn_" + treeNode.id).length > 0) return;
    var detStr = "<span type='button' class='button remove mybtn' id='delBtn_" + treeNode.id
        + "' title='删除区块' ></span>";
    aObj.append(detStr);
    var btn = $("#delBtn_" + treeNode.id);
    if (btn) btn.bind("click", function () {
        var block_id = treeNode.block_html_id;
        core.deleteBlock(block_id);
    });
}


var ztree_helper = {
    loaded: function (fun) {
        var t;
        if (!treeObj) {
            t = setTimeout(function () {
                ztree_helper.loaded(fun);
            }, 100);
        } else {
            clearTimeout(t);
            fun();
        }
    }
}
